package service.impl;

import dao.IManagerDao;
import model.Manager;
import model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import service.IManagerService;

import java.util.List;
@Service
public class ManagerService implements IManagerService {
    @Autowired
    private IManagerDao managerDao;
    @Override
    public Manager login(Manager manager) {
        if(managerDao.query(manager).size()!=0){
            return  managerDao.query(manager).get(0);
        }else{
            return  null;
        }
    }

    @Override
    public List<Manager> list(Manager manager) {
        return  managerDao.query(manager);
    }

    @Override
    public int add(Manager manager) {
        if(manager.getmPass()==null||manager.getmPass().length()==0){
            manager.setmPass("123456");
        }
        String count =String.valueOf(managerDao.add(manager)) ;
        if(count != "0"){
            return 1;
        }else{
            return 0;
        }

    }

    @Override
    public int on(Manager manager) {
        String count =String.valueOf(managerDao.on(manager)) ;
        if(count != "0"){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public int off(Manager manager) {
        String count =String.valueOf(managerDao.off(manager)) ;
        if(count != "0"){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public ModelAndView totalCount(Manager manager) {
        int totalCount = managerDao.totalCount(manager);
        System.out.println("总行数："+totalCount);
        int totalPage;
        if (totalCount % manager.getRowCount() == 0) {
            totalPage = (int) totalCount / manager.getRowCount();
            ;
        } else {
            totalPage = (int) totalCount / manager.getRowCount() + 1;
        }
        ModelAndView mv = new ModelAndView();
        List<Manager> list = managerDao.query(manager);
        mv.addObject("list", list);
        mv.addObject("totalPage", totalPage);
        mv.addObject("pageNo", manager.getPageNo());
        mv.setViewName("manager/admin_list");
        return  mv;

    }



}
